package com.ruoyi.background.service.impl;

import com.ruoyi.background.domain.AppActivityShow;
import com.ruoyi.background.mapper.AppActivityShowMapper;
import com.ruoyi.background.service.IAppActivityShowService;
import com.ruoyi.background.utils.REDIS_KEY;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 活动风采Service业务层处理
 *
 * @author susu
 * @date 2024-08-05
 */
@Service
public class AppActivityShowServiceImpl implements IAppActivityShowService {
    @Autowired
    private AppActivityShowMapper appActivityShowMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询活动风采
     *
     * @param id 活动风采主键
     * @return 活动风采
     */
    @Override
    public AppActivityShow selectAppActivityShowById(Long id) {
        return appActivityShowMapper.selectAppActivityShowById(id);
    }

    /**
     * 查询活动风采列表
     *
     * @param appActivityShow 活动风采
     * @return 活动风采
     */
    @Override
    public List<AppActivityShow> selectAppActivityShowList(AppActivityShow appActivityShow) {
        return appActivityShowMapper.selectAppActivityShowList(appActivityShow);
    }

    /**
     * 小程序获取活动风采的列表
     *
     * @param activityShow 查询条件
     * @return list
     */
    @Override
    public List<AppActivityShow> getWechatActivityShowList(AppActivityShow activityShow) {
        return appActivityShowMapper.getWechatActivityShowList(activityShow);
    }

    /**
     * 更新缓存列表
     */
    @Override
    public void updateShowCacheList() {
        String key = REDIS_KEY.SHOW_LIST_KEY;
        List<AppActivityShow> showList = appActivityShowMapper.getWechatActivityShowList(new AppActivityShow());
        redisCache.deleteByKey(key);
        redisCache.setCacheList(key, showList);
        redisCache.expire(key, 60, TimeUnit.MINUTES);
    }

    /**
     * 新增活动风采
     *
     * @param appActivityShow 活动风采
     * @return 结果
     */
    @Override
    public int insertAppActivityShow(AppActivityShow appActivityShow) {
        appActivityShow.setCreateTime(DateUtils.getNowDate());
        appActivityShow.setCreateBy(SecurityUtils.getUsername());
        appActivityShow.setUpdateTime(DateUtils.getNowDate());
        appActivityShow.setUpdateBy(SecurityUtils.getUsername());
        appActivityShow.setDeptId(SecurityUtils.getDeptId());
        if (appActivityShow.getReleaseType() == 0)
            appActivityShow.setReleaseTime(DateUtils.getNowDate());
        int res = appActivityShowMapper.insertAppActivityShow(appActivityShow);
        if (res > 0) {
            this.updateShowCacheList();
        }
        return res;
    }

    /**
     * 修改活动风采
     *
     * @param appActivityShow 活动风采
     * @return 结果
     */
    @Override
    public int updateAppActivityShow(AppActivityShow appActivityShow) {
        AppActivityShow show = this.selectAppActivityShowById(appActivityShow.getId());
        if (ObjectUtils.isEmpty(show)) throw new RuntimeException("对象不存在");
        if (0 != show.getDelFlag()) throw new RuntimeException("对象已删除！");
        appActivityShow.setUpdateTime(DateUtils.getNowDate());
        appActivityShow.setUpdateBy(SecurityUtils.getUsername());
        int res = appActivityShowMapper.updateAppActivityShow(appActivityShow);
        if (res > 0) {
            this.updateShowCacheList();
        }
        return res;
    }

    /**
     * 修改活动风采状态
     *
     * @param id     id
     * @param status 状态
     * @return 结果
     */
    @Override
    public int changeStatus(Long id, Integer status) {
        AppActivityShow show = this.selectAppActivityShowById(id);
        if (ObjectUtils.isEmpty(show)) throw new RuntimeException("对象不存在");
        if (0 != show.getDelFlag()) throw new RuntimeException("对象已删除！");
        AppActivityShow activityShow = new AppActivityShow();
        activityShow.setId(id);
        activityShow.setStatus(status);
        activityShow.setUpdateTime(DateUtils.getNowDate());
        activityShow.setUpdateBy(SecurityUtils.getUsername());
        int res = appActivityShowMapper.updateAppActivityShow(activityShow);
        if (res > 0) {
            this.updateShowCacheList();
        }
        return res;
    }

    /**
     * 批量删除活动风采
     *
     * @param ids 需要删除的活动风采主键
     * @return 结果
     */
    @Override
    public int deleteAppActivityShowByIds(Long[] ids) {
        int res = appActivityShowMapper.deleteAppActivityShowByIds(ids);
        if (res > 0) {
            this.updateShowCacheList();
        }
        return res;
    }

    /**
     * 删除活动风采信息
     *
     * @param id 活动风采主键
     * @return 结果
     */
    @Override
    public int deleteAppActivityShowById(Long id) {
        int res = appActivityShowMapper.deleteAppActivityShowById(id);
        if (res > 0) {
            this.updateShowCacheList();
        }
        return res;
    }
}
